package com.xxx.video.mine.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.adapters.SearchResultListAdapter;
import com.xxx.video.main.beans.SearchListBean;
import com.xxx.video.mine.adapters.SearchResultRecommendListAdapter;
import com.xxx.video.mine.beans.SearchHotWordListBean;
import com.xxx.video.mine.beans.SearchRecommendListBean;
import com.xxx.video.mine.presenters.ISearchResultListView;
import com.xxx.video.mine.presenters.SearchResultListPresenter;

import java.util.Objects;

import butterknife.BindView;

public class SearchResultListFragment extends BaseFragment<SearchResultListPresenter> implements ISearchResultListView,
        RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener, BaseRecycleViewAdapter.OnItemClickListener<SearchListBean.PageBean.ListBean> {

    @BindView(R.id.rcv_search_result_list)
    RefreshRecyclerView rcvSearchResultList;
    @BindView(R.id.ll_search_result_recommend_list)
    LinearLayout llSearchResultRecommendList;
    @BindView(R.id.rcv_search_result_recommend_list)
    RecyclerView rcvSearchResultRecommendList;
    /**
     * this is search Content
     */
    private String searchContent;
    /**
     * page
     */
    private int page = 1;
    private SearchResultListAdapter mSearchResultListAdapter;
    private SearchResultRecommendListAdapter mSearchResultRecommendListAdapter;
    private SearchHotSearchListAdapter mSearchHotSearchListAdapter;

    public static SearchResultListFragment newInstance(Bundle args) {
        SearchResultListFragment fragment = new SearchResultListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SearchResultListPresenter createPresenter() {
        return new SearchResultListPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_search_result_list;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().search(searchContent, page);
        getPresenter().getRecommendList();
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().search(searchContent, page);
    }

    /**
     * set search key words
     *
     * @param searchContent keywords
     */
    public void setSearchKeyWords(String searchContent) {
        page = 1;
        if (rcvSearchResultList != null) {
            rcvSearchResultList.setLoadMoreEnabled(true);
        }
        this.searchContent = searchContent;
        if (getPresenter() != null) {
            getPresenter().search(searchContent, page);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        page = 1;
        rcvSearchResultList.setPullRefreshEnabled(false);
        rcvSearchResultList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcvSearchResultList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        mSearchResultListAdapter = new SearchResultListAdapter(Objects.requireNonNull(getActivity()));
        mSearchResultListAdapter.setOnItemClickListener(this);
        rcvSearchResultList.setAdapter(mSearchResultListAdapter);
    }

    @Override
    public void initData2View(SearchListBean successOb) {
        showLayoutStatus(1);
        rcvSearchResultList.resetState();
        llSearchResultRecommendList.setVisibility(View.GONE);
        rcvSearchResultList.setVisibility(View.VISIBLE);
        if (successOb.page == null || ListUtils.isEmpty(successOb.page.list)) {
            if (page == 1) {
                llSearchResultRecommendList.setVisibility(View.VISIBLE);
                rcvSearchResultList.setVisibility(View.GONE);
            }
            return;
        }
        if (page == 1) {
            if (mSearchResultListAdapter != null) {
                mSearchResultListAdapter.setData(successOb.page.list);
            }
        } else {
            if (successOb.page.list.size() < 15) {
                rcvSearchResultList.setLoadMoreEnabled(false);
                rcvSearchResultList.setNoMoreData();
            }
            if (mSearchResultListAdapter != null) {
                mSearchResultListAdapter.addData(successOb.page.list);
            }
        }
    }

    @Override
    public void onRecyclerviewRefresh() {

    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().search(searchContent, page);
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void initRecommendData2View(SearchRecommendListBean successOb) {
        rcvSearchResultRecommendList.setLayoutManager(new LinearLayoutManager(getActivity()));
        ConcatAdapter concatAdapter = new ConcatAdapter();
        rcvSearchResultRecommendList.setAdapter(concatAdapter);
        mSearchHotSearchListAdapter = new SearchHotSearchListAdapter(Objects.requireNonNull(getActivity()));
        concatAdapter.addAdapter(mSearchHotSearchListAdapter);
        if (mSearchResultRecommendListAdapter == null) {
            mSearchResultRecommendListAdapter = new SearchResultRecommendListAdapter(Objects.requireNonNull(getActivity()));
            concatAdapter.addAdapter(mSearchResultRecommendListAdapter);
            mSearchResultRecommendListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<SearchRecommendListBean.ListBean>) (listBean, position, type) -> startVideoDetailsActivity(listBean.fContentId));
        }
        mSearchResultRecommendListAdapter.setData(successOb.list);
        getPresenter().getHistoryKeyword();
    }

    @Override
    public void initSearchHotWordListBeanData2View(SearchHotWordListBean successOb) {
        if (mSearchHotSearchListAdapter != null) {
            mSearchHotSearchListAdapter.setData(successOb.list);
            mSearchHotSearchListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<SearchHotWordListBean.ListBean>) (listBean, position, type) -> {
                page = 1;
                searchContent = listBean.fKeyword;
                getPresenter().search(searchContent, page);
            });
        }
    }

    @Override
    public void onItemClick(SearchListBean.PageBean.ListBean listBean, int position, int type) {
        startVideoDetailsActivity(listBean.fId);
    }
}
